package com.dounion.sample.controller;

import com.dounion.framework.entity.Model;
import com.dounion.framework.helper.RequestHelper;
import com.dounion.framework.helper.ResponseBuilder;
import com.dounion.sample.entity.TsDetails;
import com.dounion.sample.service.StockService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/stock")
public class StockController {

    @Resource
    StockService stockService;

    /**
     * 更新指数
     *
     * @return
     */
    @RequestMapping("/save")
    public Object save() {
        Model input = RequestHelper.getInput();
        return stockService.save(input);
    }

    /**
     * 更新所有股票
     *
     * @return
     */
    @RequestMapping("/updateAll")
    public Object updateAll() {
        Model input = RequestHelper.getInput();
        return stockService.updateAll(input);
    }

    /**
     * 查询所有股票最近n天
     *
     * @return
     */
    @RequestMapping("/list")
    public Object list() {
        Model input = RequestHelper.getInput();
        List list = stockService.list(input);
        return Model.builder()
                .set("最近" + input.getInt("day", 1) + "天涨幅", list);
    }


    /**
     * 稍加交易
     *
     * @return
     */
    @RequestMapping("/addts")
    public Object addTs(TsDetails tsDetails) {
        return stockService.addTs(tsDetails);
    }


    /**
     * 稍加交易
     *
     * @return
     */
    @RequestMapping("/calculate")
    public Object calculate() {
        Model input = RequestHelper.getInput();
        return ResponseBuilder.buildSuccess(stockService.calculate(input));
    }
}
